package com.uex.dev.manage.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.dao.DeviceExtendDao;
import com.uex.dev.manage.admin.po.DeviceExtendPO;
import com.uex.dev.manage.admin.po.DevicePO;
import com.uex.dev.manage.admin.service.IDeviceExtendService;
import com.uex.dev.manage.admin.service.IDeviceService;
import com.uex.dev.manage.admin.utils.LogUtil;
import com.uex.dev.manage.admin.vo.device.DeviceDeliveryResp;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wangchao
 * @since 2024-02-22
 */
@Service
public class DeviceExtendServiceImpl extends ServiceImpl<DeviceExtendDao, DeviceExtendPO> implements IDeviceExtendService {

    @Resource
    private IDeviceService deviceService;

    @Override
    public DeviceDeliveryResp getDeliveryDevice(Integer deviceId) {
        DevicePO device = deviceService.getById(deviceId);
        Preconditions.checkArgument(ObjectUtils.isNotNull(device), "设备Id不存在");
        DeviceExtendPO deviceExtend = this.getById(deviceId);
        DeviceDeliveryResp deviceDeliveryResp = new DeviceDeliveryResp();
        if (ObjectUtils.isNotNull(deviceExtend)) {
            BeanUtils.copyProperties(deviceExtend, deviceDeliveryResp);
        }
        LogUtil.log(device.getId(), "获取设备发货及生产信息");
        return deviceDeliveryResp;
    }

    @Override
    public void updateDeliveryDevice(DeviceExtendPO deviceModExtendReq) {
        Preconditions.checkArgument(ObjectUtils.isNotNull(deviceModExtendReq.getDeviceId()), "设备Id不能为空");
        DevicePO device = deviceService.getById(deviceModExtendReq.getDeviceId());
        Preconditions.checkArgument(ObjectUtils.isNotNull(device), "设备不存在");
        DeviceExtendPO deviceExtend = this.getById(deviceModExtendReq.getDeviceId());
        // 不存在就添加
        if (deviceExtend == null) {
            Preconditions.checkArgument(deviceModExtendReq.getDeviceProductTime() != null, "设备生产时间不能为空");
            this.save(deviceModExtendReq);
            LogUtil.log(deviceModExtendReq.getDeviceId(), "添加设备发货信息");
        }else{
            LogUtil.log(deviceModExtendReq.getDeviceId(), "更新设备及发货信息") ;
            this.updateById(deviceModExtendReq);
        }
    }
}
